<template>
    <section>


        <!--添加框-->
        <el-dialog title="" :visible.sync="dialogFormVisible">

            <el-form :model="addForm" ref="addForm">


                <el-form-item label="服务名" :label-width="formLabelWidth" prop="sn">
                    <el-input v-model="addForm.name" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="售价" :label-width="formLabelWidth" prop="sn">
                    <el-input v-model="addForm.saleprice" autocomplete="off"></el-input>
                </el-form-item>


                <el-form-item label="成本价" :label-width="formLabelWidth" prop="sn">
                    <el-input v-model="addForm.costprice" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item prop="resources" label="产品图片" :label-width="formLabelWidth">
                    <el-upload
                            class="upload-demo"
                            ref="upload"
                            action="/api/fastdfs/upload/"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <!--:auto-upload="false"-->
                        <el-button size="small" type="primary">选取文件</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不能超过500kb</div>
                    </el-upload>
                </el-form-item>

                <el-form-item label="详情" :label-width="formLabelWidth" prop="intro">
<!--                    <el-input v-model="addForm.intro" autocomplete="off"></el-input>-->
                    <div class="edit_container">
                        <quill-editor :options="editorOption" v-model="addForm.intro"></quill-editor>
                    </div>
                </el-form-item>
                <el-form-item label="预约通知" :label-width="formLabelWidth" prop="orderNotice">
<!--                    <el-input v-model="addForm.orderNotice" autocomplete="off"></el-input>-->
                    <!--使用富文本编辑器-->
                    <quill-editor :options="editorOption" v-model="addForm.orderNotice"></quill-editor>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('addForm')">取 消</el-button>
                <el-button type="primary" @click="onSubmit('addForm')">确 定</el-button>
            </div>
        </el-dialog>

        <!--高级查询添加-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-input v-model="dname" placeholder="服务名"></el-input>
                </el-form-item>

                <el-select v-model="value" clearable placeholder="请选择状态">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>

                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="flag1()" @click="up">上架
                    </el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="flag()" @click="down">下架
                    </el-button>
                </el-form-item>

            </el-form>
        </el-col>
        <!--列表展示-->
        <el-table :data="products" highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="index" width="50"></el-table-column>
            <el-table-column prop="name" label=" 服务名" width="110"></el-table-column>
            <!--<el-table-column prop="resources" label="图片" width="110">
                <template scope="scope">
                    <img :src="scope.row.logo" width="50px" height="50px" class="head_pic"/>
                </template>
            </el-table-column>-->
            <el-table-column prop="saleprice" label="售价" width="110"></el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" width="110"></el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" width="110"></el-table-column>

            <el-table-column prop="state" label="状态" width="110">
                <template scope="scope">
                    <span style="color: deepskyblue" v-if="scope.row.state===1">上架</span>
                    <span style="color: mediumvioletred" v-else-if="scope.row.state===0">下架</span>
                </template>
            </el-table-column>
            <el-table-column prop="costprice" label="成本价" width="110"></el-table-column>
            <el-table-column prop="createtime" label="创建时间" width="110"></el-table-column>
            <el-table-column prop="salecount" label="销量" width="110"></el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">

            <el-button type="success" @click="batchUpdate" :disabled="flag2()">批量上架</el-button>
            <el-button type="danger" @click="batchdown" :disabled="flag3()">批量下架</el-button>
            <el-button type="danger" @click="batchdelete" :disabled="this.sels.length===0">批量删除</el-button>

            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

    </section>
</template>

<script>
    /*富文本编辑器*/
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    /*图片再富文本里面上传，返回上传的url*/
    import quillconfig from "../../common/quill-config"
    export default {
        name: "Department",
        /*暴露组件*/
        components: {
            quillEditor
        },
        data() {
            return {
                //引用的quill-config.js
                editorOption: quillconfig,
                state: "",
                //回显图片
                fileList: [],
                //高级查询字段
                value: "",
                dname: '',
                //上架下架
                options: [{
                    value: '0',
                    label: '下架'
                }, {
                    value: '1',
                    label: '上架'
                }],
                //弹出框的标题
                title: '',
                //类型列表展示
                products: [],
                //页面加载
                listLoading: '',
                currentPage: 1,
                pageSize: 6,
                total: 0,

                //选中的数据
                sels: [],
                //dialog显示
                dialogFormVisible: false,
                addForm: {
                    id: "",
                    name: "",
                    resources: "",
                    saleprice: "",
                    //成本价
                    costprice: "",
                    intro: "",
                    //预约通知
                    orderNotice: ""

                },
                formLabelWidth: '120px'
            }
        },
        methods: {
            flag() {
                if (this.sels.length===0){
                    return true
                }
                if (this.sels.length > 1) {
                    return true
                }
                if (this.sels.length > 0) {
                    if (this.sels[0].state === 0) {
                        return true
                    }
                }
            },
            //判断单个上架下架
            flag1() {
                if (this.sels.length===0){
                    return true
                }
                if (this.sels.length > 1) {
                    return true
                }
                if (this.sels.length > 0) {
                    if (this.sels[0].state === 1) {
                        return true
                    }
                }
            },
            //判断批量上架下架
            flag2() {
                if (this.sels.length === 0) {
                    return true
                }
                if (this.sels.length > 0) {
                    for (let x in this.sels) {
                        if (this.sels[x].state === 1) {
                            return true
                        }
                    }
                }
            },
            flag3() {
                if (this.sels.length === 0) {
                    return true
                }
                if (this.sels.length > 0) {
                    for (let x in this.sels) {
                        if (this.sels[x].state === 0) {
                            return true
                        }
                    }
                }
            },
            //重置。点击取消
            resetForm1() {
                this.dialogFormVisible1 = false
            },
            //关闭子窗口
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            //重置。点击取消
            resetForm(addForm) {
                // this.$refs[addForm].resetFields();
                this.dialogFormVisible = false
            },/*"http://121.37.194.36"+*/
            //logo上传成功之后
            handleSuccess(response, file, fileList) {
                console.log(fileList)
                //上传成功之后，给表单的resources设置
                for (let i = 0; i < fileList.length; i++) {
                    if (i == 0) {
                        this.addForm.resources = fileList[i].response.resultObj;
                    } else {
                        this.addForm.resources = this.addForm.resources + "," + fileList[i].response.resultObj
                    }
                }


            },
            //清空图片
            handleRemove(file, fileList) {
                /*var filePath = file.response.resultObj;
                this.$http.delete("/common/fastDfs/?path=" + filePath).then(res => {
                        if (res.data.success) {
                            this.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '删除失败!',
                                type: 'error'
                            });
                        }
                    })*/

            },
            //提交
            onSubmit(addForm) {
                /*       //手动提交
                       this.$refs.upload.submit();*/
                let productDetail = {intro: this.addForm.intro, orderNotice: this.addForm.orderNotice}
                this.addForm.productDetail = productDetail
                //判断是否有id有就是修改,否则就是添加
                this.$http.put("/product", this.addForm).then((res) => {
                    if (res.data.success) {
                        this.dialogFormVisible = false;
                        this.$message({
                            message: '操作成功!',
                            type: 'success'
                        });
                        //重置表单
                        this.$refs['addForm'].resetFields();
                        this.loadData()
                    } else {
                        this.dialogFormVisible = false;
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                });
            },
            //高级查询
            getUsers() {
                //跳转到第一页查询
                this.currentPage = 1
                this.loadData()
            },
            //修改
            handleEdit: function (index, row) {
                console.log(row)
                //清空图片
                this.fileList = [];
                this.title = "修改";
                //弹出dialog
                this.dialogFormVisible = true;
                //回显数据(一般数据)
                for (let x in this.addForm) {
                    this.addForm[x] = row[x]
                }
                this.addForm.intro = row.productDetail.intro;
                this.addForm.orderNotice = row.productDetail.orderNotice;
                //回显图片
                console.log(row.resources)
                if (row.resources) {
                    //分割
                    let split = row.resources.split(",");
                    for (let i = 0; i < split.length; i++) {
                        //存放到回显图片的集合中
                        this.fileList.push({"url": "http://121.37.194.36" + split[i]})

                    }
                }

            },
            //删除
            handleDel(index, row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.delete("/product/" + row.id).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })

            },
            //增加
            handleAdd() {
                this.title = "添加";

                //清空表单
                for (let i in this.addForm) {
                    // console.log(addFormKey)
                    this.addForm[i] = ""
                }
                //显示修改框
                this.dialogFormVisible = true;
                //清空图片
                this.fileList = [];
            },
            //加载数据
            loadData() {
                let params = {
                    localPage: this.currentPage, //加载默认参数
                    pageSize: this.pageSize,
                    keywords: this.dname,
                    state: this.value
                }
                //请求地址  then请求成功  catch请求失败
                this.$http.post("/product", params).then(res => {
                    let {data, totals} = res.data; //解构
                    this.products = data;
                    // console.log(data)
                    /*
                      for (let x in this.products) {
                          this.products[x].resources = "http://121.37.194.36" + this.products[x].resources
                      }*/
                    this.total = totals;
                }).catch()
            },
            //批量上架
            batchUpdate() {
                this.$confirm('批量上架这些数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/product/batchUpdate", this.sels).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //批量下架
            batchdown() {
                this.$confirm('批量下架这些数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/product/batchDown", this.sels).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //点击分页,更改当前页
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadData()
            },
            //选择多少行的数据
            selsChange(rows) {
                this.sels = rows;
                console.log(this.sels)
            },
            //上架
            up() {
                console.log(this.sels[0].id)
                this.$confirm('上架该商品吗?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/product/up/" + this.sels[0].id).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //下架
            down() {
                this.$confirm('下架该商品吗?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/product/down/" + this.sels[0].id).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            },
            //批量删除batchDelete
            batchdelete() {
                this.$confirm('批量删除这些数据, 是否继续?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//确认
                    this.$http.post("/product/batchDelete", this.sels).then(res => {
                        console.log(res);
                        let {message, success} = res.data; //解构函数
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.loadData()
                        }
                    })
                }).catch(() => {
                    this.$message('已经取消');
                })
            }
        },
        mounted() {
            //加载完毕之后执行
            this.loadData()
        }
    }
</script>

<style scoped>

</style>
